include<ub.scad>; //⇒ v.gd/ubaer or https://github.com/UBaer21/UB.scad
/*[Hidden]*/
designVersion="1.6";
designer="Ulrich Bär";
license="CC0";
useVersion=22.222;//(sites.google.com/site/ulrichbaer)
assert(Version>=useVersion,str("lib version ",Version," detected, install ",useVersion," ub.scad library‼ ⇒http://v.gd/ubaer"));
/*[Basics]*/
nozzle=.2;
bed=false;
pPos=[0,0];
info=false;
name=undef;
fn=20;

/*[Clicker]*/
duo=true;
//center ratchets
e=9;
// multiplier position ratchets
n=2;
// diameter
d=12;
// wheel height 
h=3;
// bottom base plate 
bottom=1.5;
// stabilizer for thin bases
bridge=false;

dist=1.3*d;


// Single
if(!duo)T(printPos)PrevPos(rot=0,t=[0,0,-bottom+.75])Color("skyblue")Tz(bottom-.75)Clicker(h=h+2,end=true);
if(!duo)T(printPos)PrevPos(rot=0,t=[0,d*2,(h+2)/2-h/2])T(y=-d*2)ClickerWheel();


// Duo
if(duo)T(printPos)PrevPos(rot=0,t=[0,0,-bottom+.75])Tz(bottom-.75)Color("gold")Polar(2,-dist)Clicker(h=h+2);
if(duo)T(printPos)PrevPos(rot=0,t=[dist,d*2,(h+2)/2-h/2])T(y=-d*2)ClickerWheel();
if(duo)if($preview)translate([-dist,0,(h+2)/2-h/2])ClickerWheel();

if(bridge&&duo)T(printPos)PrevPos(t=[0,0,h+2+bottom])Bridge();


module Bridge(dist=dist,h=[0.1,h+2]){

Halb()MKlon(-dist)difference(){
Tz(bottom)Pin(d=d -3-spiel*2,l=h,center=true,cut=[0,7]);
cylinder(50,center=true,d=d-5.5);
Kegel(d1=d -5.5+1);
Tz(h[1]+bottom+1)R(180)Kegel(d1=d -4+1);
}
  Roof(bottom,[1,1]*min(.5,bottom/2-.15)){
    
    difference(){
      Rund(0,5){
        MKlon(-dist)Kreis(d=d+3,fs=fs);
        Quad(d/2+dist*2,10,center=[1,1],r=0);
      }
       MKlon(-dist) Kreis(d=d -5.5,fs=fs);
      T(d/4+3,0.75)rotate(30)Rund(.5)circle(3,$fn=3);
      T(-d/4-3,-0.75)rotate(-30)Rund(.5)circle(3,$fn=3);
    }
  }
}

module Clicker(e=e,n=n,d=d,h=h,end=false){

deg=80/e;

 Roof(h,.5*[1,1]) difference(){
    Rund(.5,1,fn=e*3)Star(e=e,r1=d/2,r2=d/2+1.5,grad=[deg,-deg]);
    //Rund(1.5,0,fn=fn)Polar(n,1)Kreis(d=d-5,grad=360/n,fn=12);
    Kreis(d=d -3,fs=fs);
  }
  intersection(){
    Tz(h/2)MKlon(tz=-h/2)Kegel(d2=d,d1=d +4.5,grad=30,fn=60);
    Roof(h,.5*[1,1])Ring(50,center=true,id=d -2,d=d +2,2D=true,fn=60);
  }
  Tz(-bottom+.75)Roof(bottom,[1,1]*min(.5,bottom/2-.15)){
    
    difference(){
      Rund(0,5){
        Kreis(d=d+3,fs=fs);
        Quad(d/2+(end?13:11),10,center=[0,1],r=0);
        if(end)Rund(1)Kreis(d=d +29,grad=45,rand=5);
      }
      Kreis(d=d -2,fs=fs);
      T(d/2+6,0.75)rotate(30)Rund(.5)circle(3,$fn=3);
    }
  }
  if(end){
  //T(d/2+12.6)Prisma(5,10,h,c1=4);
  Tz((h+bottom-.75)/2-bottom+.75)rotate(-22.5)RingSeg(r=d/2+12,size=5,h=h+bottom-.75,grad=45,spiel=0);
  }
}

module ClickerWheel(e=e,n=n,d=d+1,h=h){
d2=d +10;
echo(Teiler=teiler(e));

  Roof(h,.25*[1,1])
  Rund(.2,.5){
    Polar(n*teiler(e),rot=5)Involute(r=d/2,grad=95,s=0.45,delta=0.85,fn=10);
  //Kreis(d=d2,rand=-2,fn=e*3);
    Rund(1,2)Star(e*n,d2/2+2.1,d2/2+3.5,d=d2-.5,fn2=e*5);
  }
}

// version Info
if(designVersion)T(printPos)color("navy")linear_extrude(.1)Seg7(str(designVersion),h=1,spiel=0.01,b=.05,ratio=0.5,center=true,name=0);